package com.runner.entity;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.runner.utility.Constants;
import com.runner.utility.Locator;

public class GraphicsComponent implements Constants {

	protected Sprite sprite;
	protected float direction;
	protected float scale;
	protected Vector2 position;

	public GraphicsComponent(Vector2 pos, String path, float scale) {
		sprite = new Sprite(Locator.getAssets().get(path, Texture.class));
		direction = 0;
		this.position = pos;
		this.scale = scale;
	}

	public void render(SpriteBatch sb, Entity entity) {
		sb.draw(sprite.getTexture(), entity.renderXPosition(sprite.getWidth()), entity.renderYPosition(sprite.getHeight()), sprite.getWidth() * 0.5f,
				sprite.getHeight() * 0.5f, sprite.getWidth(), sprite.getHeight(), SCALE * scale * Locator.getxSpriteScale(),
				SCALE * scale * Locator.getySpriteScale(), direction, 0, 0, (int) sprite.getWidth(), (int) sprite.getHeight(), false, false);
	}

	public void render(SpriteBatch sb, TextureRegion currentFrame, Entity entity) {
		sb.draw(currentFrame, entity.renderXPosition(currentFrame.getRegionWidth()), entity.renderYPosition(currentFrame.getRegionHeight()),
				currentFrame.getRegionWidth() * 0.5f, currentFrame.getRegionHeight() * 0.5f, currentFrame.getRegionWidth(),
				currentFrame.getRegionHeight(), Constants.SCALE * scale * Locator.getxSpriteScale(),
				Constants.SCALE * scale * Locator.getySpriteScale(), 0);
	}

	public void render(SpriteBatch sb, Sprite sp, Entity entity) {
		sb.draw(sp.getTexture(), entity.renderXPosition(sprite.getWidth()), entity.renderYPosition(sprite.getHeight()), sp.getWidth() * 0.5f, sp.getHeight() * 0.5f, sp.getWidth(),
				sp.getHeight(), SCALE * scale * Locator.getxSpriteScale(), SCALE * scale * Locator.getySpriteScale(), direction, 0, 0,
				(int) sp.getWidth(), (int) sp.getHeight(), false, false);
	}

	// this render function renders sprites with an alpha
	public void render(SpriteBatch sb, float alpha, Entity entity) {
		sb.setColor(sb.getColor().r, sb.getColor().g, sb.getColor().b, alpha);
		
		sb.draw(sprite.getTexture(), entity.renderXPosition(sprite.getWidth()), entity.renderYPosition(sprite.getHeight()), sprite.getWidth() * 0.5f, sprite.getHeight() * 0.5f,
				sprite.getWidth(), sprite.getHeight(), SCALE * scale, SCALE * scale,
				direction, 0, 0, (int) sprite.getWidth(), (int) sprite.getHeight(), false, false);
		
		sb.setColor(sb.getColor().r, sb.getColor().g, sb.getColor().b, 1.0f);
	}

	public void dispose() {
		sprite.getTexture().dispose();
	}

	public Vector2 getPosition() {
		return position;
	}

	public float getDirection() {
		return direction;
	}

	public void setPosition(Vector2 position2) {
		position.set(position2);
	}

	public void setDirection(float direction2) {
		direction = direction2;
	}

	public void setSprite(Sprite sprite2) {
		sprite = sprite2;
	}

	public Sprite getSprite() {
		return sprite;
	}
}
